﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using System.Data;

namespace Besture.UI
{
    public partial class HotelBrandSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {
			//加载下拉框
			var dtParentID = QueryDac.GetTable("SELECT CategoryName,ID FROM dbo.TBHotelsCategory WHERE CategoryType=5 AND ParentID=0");
			ControlHelper.BindDropDownList(ddlParentID, dtParentID, "CategoryName", "ID");
			//加载下拉框
			/*var dtCategoryType = QueryDac.GetTable("SELECT Name,CODE FROM dbo.TBDictionaryType WHERE CODE IN (5) AND TYPE=0");
			ControlHelper.BindDropDownList(ddlCategoryType, dtCategoryType, "Name", "CODE");*/
			
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            if (id == 0) return;

            var dac = new HotelsCategoryDac();
            var model = dac.GetModel(id);
			if(model==null) return;

            txtCategoryName.Text = model.CategoryName.ToString();
			ddlParentID.SelectedValue = model.ParentID.ToString() == "" ? "" : model.ParentID.ToString();
			//ddlCategoryType.SelectedValue = model.CategoryType.ToString() == "" ? "" : model.CategoryType.ToString();
			txtSortID.SortValue = model.SortID != 0 ? model.SortID : 0;
			

			
        }
        #endregion

        #region 保存

        protected void BtnSubmitClick(object sender, EventArgs eventArgs)
        {
            var dac = new HotelsCategoryDac();
            HotelsCategoryModel model=null;
            var baseDac = new BaseDac<HotelsCategoryModel>();
            //添加
            if (!EditPage)
            {
                model = new HotelsCategoryModel();
                model.CategoryName = txtCategoryName.Text.Trim();
				model.ParentID = ddlParentID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlParentID.SelectedValue);
                model.CategoryType = Constants.HOTEL_CATEGORY_BRAND;
				model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;
				

                //加载添加基本数据
                baseDac.LoadAddBaseModel(model);
				

                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(ID);
                model.CategoryName = txtCategoryName.Text.Trim();
				model.ParentID = ddlParentID.SelectedValue == "" ? 0 : Convert.ToInt32(ddlParentID.SelectedValue);
				model.CategoryType = 5;
				model.SortID = txtSortID.SortValue != 0 ? txtSortID.SortValue : 0;
				

                //加载修改基本数据
                baseDac.LoadUpdateBaseModel(model);
				

                dac.Update(model);
            }

            FunHelper.Alert("保存成功", ListUrl());
        }
        #endregion
    }
}